/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Validators;

import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator("numberValidator")
public class NumberValidator implements Validator {

    private static final Pattern PATTERN = Pattern.compile("[0-9]{10}");

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value == null) {
            return; 
        }

        if (!PATTERN.matcher(value.toString()).matches()) {
            FacesMessage msg = new FacesMessage("Validación fallida.", 
						"Campo incorrecto.");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }

}